const app = require('express')();
const fs = require('fs');
const path = require('path');
const http = require('../../utils/http');
const emitter = require('../../utils/events');
const { readExcelFile, writeExcelFile } = require('../../utils/excelFile');

// 页面基础地址
let baseURL = 'https://www.baidu.com/s?ie=utf-8&tn=baidu&wd=';

// 请求头设置
let header = {
  Accept:
    'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
  'Cache-Control': 'no-cache',
  Connection: 'keep-alive',
  Host: 'www.baidu.com',
  Cookie:
    'MSA_WH=375_667; PSTM=1657856728; BAIDUID=1364780B637966236A78AE478970E8E4:SL=0:NR=10:FG=1; sug=3; sugstore=1; ORIGIN=0; bdime=0; BD_UPN=12314753; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BIDUPSID=00151BDAA7F84F9A5A64BADA44DCEC61; H_PS_PSSID=36543_37360_36807_36786_37534_37499_37575_26350_37370; ispeed=2; ispeed_lsm=60; BAIDUID_BFESS=1364780B637966236A78AE478970E8E4:SL=0:NR=10:FG=1; delPer=0; BD_CK_SAM=1; PSINO=7; BA_HECTOR=04012g052k2524a1842hdm7d1hkq5ir1b; ZFY=YL:BdOMZBVW8W:As:B4cIgTEGCsohMjNQ9ekzwIADI8qOg:C; channel=baidusearch; baikeVisitId=4e75860e-e03a-4def-9654-628a0ae549c4; H_PS_645EC=7fa9WUduQT5%2FFG8aBd%2F3o2bZAFU8dgJDwXWGtZchanWdhIiYOKDy47yWXRc',
  Pragma: 'no-cache',
  'sec-ch-ua-platform': 'Windows',
  'Upgrade-Insecure-Requests': 1,
  'Sec-Fetch-Dest': 'document',
  'Sec-Fetch-Mode': 'navigate',
  'Sec-Fetch-Site': 'same-origin',
  'User-Agent':
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
};

const whitelist = ['企查查', '天眼查', '爱企查']; // 白名单

// 等待时间
function sleep(time) {
  return new Promise(reslove => setTimeout(reslove, time));
}

// 判断目录是否单文件
function isDirOne(dirname) {
  return fs.promises.readdir(dirname).then(files => {
    return files.length === 1;
  });
}

// 读取目录中第一个文件
function readOneFile(dirname) {
  return fs.promises.readdir(dirname).then(files => {
    return files[0];
  });
}

// 处理爬取表格数据
async function handleExcelData(data) {
  // 存放最终查找数据
  const excelData = [];
  let index = 0; // 第一个数据
  let count = data.length; // excel 数据总数
  searchPageData(data[index][0]); // 第一条数据开始查找
  // 查找当前页面
  async function searchPageData(companyName) {
    const url = `${baseURL}${encodeURI(companyName)}`;
    header.Referer = url;
    console.log('查询地址:', url);
    try {
      if (index) {
        await sleep(3000); // 等待时间
      }
      const $ = await http(url, header); // 获取页面
      emitter.emit('findUrl_event', url); // 发布当前查找地址事件
      const ele = $('#content_left').find('.result.c-container .c-container .c-title>a'); // 查找条件

      // console.log(companyName);
      // console.log(222, ele.text());

      if (!ele) {
        excelData.push([companyName, '暂无查到']);
        index++;
        if (index >= count) {
          console.log(excelData);
          emitter.emit('allSuccess_event', '数据全部爬取成功!!'); // 处理完成时事件
          writeExcelFile(excelData);
          console.log('全部结束', 11);
        } else {
          emitter.emit('sumSuccess_event', excelData.length); // 发布处理数据条数
          // searchPageData(data[index][0]);
        }
      } else {
        const searchData = [companyName]; // 查找数据
        // 遍历所有数据
        ele.each((i, el) => {
          if (el.children.length === 1) {
            const text = el.children[0].data;
            if (text) {
              whitelist.forEach(item => {
                if (text.indexOf(item) > -1) {
                  searchData.push(text); // 添加查询结果
                }
              });
            }
          }
        });
        if (searchData.length === 1) {
          searchData.push('暂无数据');
        }
        excelData.push(searchData);
        index++;
        if (index >= count) {
          console.log(excelData);
          emitter.emit('allSuccess_event', '数据全部爬取成功!!'); // 处理完成时事件
          writeExcelFile(excelData);
          console.log('全部结束', 22);
        } else {
          emitter.emit('sumSuccess_event', excelData.length); // 发布处理数据条数
          // searchPageData(data[index][0]);
        }
      }
    } catch (err) {
      index++; // 下一条
      excelData.push([companyName, '此条数据爬取出错', err.toString()]); //  添加错误数据
      // 发送一条错误消息
      // emitter.emit('error_event', url);
      console.log('error', 111);
      // 判断是否结束
      if (index < count) {
        emitter.emit('sumSuccess_event', excelData.length); // 发布处理数据条数
        // searchPageData(data[index][0]); // 继续下一条数据
      } else {
        console.log(excelData);
        emitter.emit('allSuccess_event', '数据全部爬取成功!!'); // 处理完成时事件
        writeExcelFile(excelData);
        console.log('全部结束', 33);
      }
    }
  }
}

// 处理爬虫
async function handleReptile(req, res) {
  const excelPath = path.join(__dirname, '../../../output'); // 存储上传文件目录
  const isOneDir = await isDirOne(excelPath); // 目录中是否单文件
  if (!isOneDir) {
    return res.api_error('请重新上传文件在进行爬虫');
  }
  res.api_success(true, '正在努力的爬取数据！！！请稍等！！');
  // 后续处理
  const fileName = await readOneFile(excelPath); // 获取目录中文件
  const filePath = path.join(excelPath, fileName); // 上传目录完整路径
  const excelData = readExcelFile(filePath); // 读取上传 excel 数据
  handleExcelData(excelData);
}

app.post('/', (req, res) => handleReptile(req, res));

module.exports = app;
